from .client import Client
from .consts import *
from .utils import get_all_data, api_key, secret_key, passphrase, flag


class MarketAPI(Client):

    def __init__(self, api_key, api_secret_key, passphrase, use_server_time=False, flag='1'):
        Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag)

    # Get Tickers
    def get_tickers(self, instType, uly=''):
        if uly:
            params = {'instType': instType, 'uly': uly}
        else:
            params = {'instType': instType}
        return self._request_with_params(GET, TICKERS_INFO, params)

    # Get Ticker
    def get_ticker(self, instId):
        params = {'instId': instId}
        return self._request_with_params(GET, TICKER_INFO, params)

    # Get Index Tickers
    def get_index_ticker(self, quoteCcy='', instId=''):
        params = {'quoteCcy': quoteCcy, 'instId': instId}
        return self._request_with_params(GET, INDEX_TICKERS, params)

    # Get Order Book
    def get_orderbook(self, instId, sz=''):
        params = {'instId': instId, 'sz': sz}
        return self._request_with_params(GET, ORDER_BOOKS, params)

    # Get Candlesticks
    @get_all_data
    def get_candlesticks(self, instId, after='', before='', bar='', limit='', **kwargs):
        params = {'instId': instId, 'after': after, 'before': before, 'bar': bar, 'limit': limit}
        return self._request_with_params(GET, MARKET_CANDLES, params)

    # GGet Candlesticks History（top currencies only）
    @get_all_data
    def get_history_candlesticks(self, instId, after='', before='', bar='', limit='', **kwargs):
        """
        1. 获取交易产品历史K线数据接口不会返回没有画完的K线，但是获取交易产品K线数据接口会返回
        2. 获取交易产品历史K线数据接口可以获取更久的历史K线，而获取交易产品K线数据接口只会返回最近1440条数据
        3. 获取交易产品历史K线数据接口单次最多只能获取100条数据，但是获取交易产品K线数据接口单次最多可以获取300条数据

        '币币和永续可以拿到2019年10月1日之后的数据，交割可以拿到2019年7月1日之后的数据的'

        :param instId:
        :param after:
        :param before:
        :param bar:
        :param limit:
        :param kwargs:
        :return:
        """
        params = {'instId': instId, 'after': after, 'before': before, 'bar': bar, 'limit': limit}
        return self._request_with_params(GET, HISTORY_CANDLES, params)

    # Get Index Candlesticks
    @get_all_data
    def get_index_candlesticks(self, instId, after='', before='', bar='', limit='', **kwargs):
        params = {'instId': instId, 'after': after, 'before': before, 'bar': bar, 'limit': limit}
        return self._request_with_params(GET, INDEX_CANSLES, params)

    # Get Mark Price Candlesticks
    @get_all_data
    def get_markprice_candlesticks(self, instId, after='', before='', bar='', limit='', **kwargs):
        params = {'instId': instId, 'after': after, 'before': before, 'bar': bar, 'limit': limit}
        return self._request_with_params(GET, MARKPRICE_CANDLES, params)

    # Get Index Candlesticks
    def get_trades(self, instId, limit=''):
        params = {'instId': instId, 'limit': limit}
        return self._request_with_params(GET, MARKET_TRADES, params)

    # Get Volume
    def get_volume(self):
        return self._request_without_params(GET, VOLUMNE)

    # Get Oracle
    def get_oracle(self):
        return self._request_without_params(GET, ORACLE)

    # Get Index Components
    def get_index_components(self, index):
        params = {'index': index}
        return self._request_with_params(GET, Components, params)

    # Get Tier
    def get_tier(self, instType='', tdMode='', uly='', instId='', ccy='', tier=''):
        params = {'instType': instType, 'tdMode': tdMode, 'uly': uly, 'instId': instId, 'ccy': ccy, 'tier': tier}
        return self._request_with_params(GET, TIER, params)

    # Get exchange rate
    def get_exchange_rate(self):
        params = {}
        return self._request_with_params(GET, BORROW_REPAY, params)

marketAPI = MarketAPI(api_key, secret_key, passphrase, False, flag)